﻿using Autofac;

using Inventory.Infrastructure.RedisHelper;
using Inventory.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System.Reflection;

namespace Inventory.API.Extensions
{

    public static class ServiceCollectionExtensions
    {

      /// <summary>
      /// 注册服务                     
      /// </summary>
      /// <param name="services"></param>
      /// <param name="configuration"></param>
      /// <returns></returns>
        public static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration)
        {
                    // 注册数据库上下文
                    services.AddDbContext<ApplicationContext>(options =>
            options.UseMySql(configuration.GetConnectionString("DefaultConnection"),
            new MySqlServerVersion(new Version(5, 7, 0)))); // 使用 MySQL 5.7
            // 注入中介者
            services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
            // 添加 RedisHelper
          //  services.AddSingleton(new RedisHelpers("45.207.208.253")); // 替换为你的 Redis 连接字符串
            // 注入AutoMapper
            services.AddAutoMapper(typeof(MapingProfile));
            
            // 注册仓储
            services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));

            //注入全局异常处理
            services.AddControllers(x => x.Filters.Add<GlobalExceptionFilter>());

            return services;
        }




   

        /// <summary>
        /// 注册 Autofac 模块
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection RegisterAutofac(this IServiceCollection services, ContainerBuilder containerBuilder)
        {
           
            containerBuilder.RegisterModule(new AutofaModuleRegister());
            return services;
        }
    }
}
                        